﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NLUNZ
{
    public partial class Test : System.Web.UI.Page
    {
        private static string webServiceUrl =
            ConfigurationManager.AppSettings["trackingApiUrl"];
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string code = TextBox1.Text;
            string number = TextBox2.Text;
            string data = LoadData(code, number);
            searchResult.Text = data;
        }
        private string LoadData(string code, string trackingNumber)
        {
            string apiKey = ConfigurationManager.AppSettings["TrackingApiKey"];
            string url = string.Format(webServiceUrl, apiKey, code, trackingNumber);
            string kuaidiUrl = "http://www.kuaidi100.com/openapi/index.shtml";
            WebResponse response;
            try
            {
                WebRequest request = WebRequest.Create(kuaidiUrl);
                request.Method = "POST";
                response = request.GetResponse();
            }
            catch (Exception ex)
            {
                return ex.Message + ex.StackTrace;
            }
            Stream stream = response.GetResponseStream();

            StreamReader reader = new StreamReader(stream);
            //string json = reader.ReadToEnd();
            return reader.ReadToEnd();
        }
    }
}